package com.rqf.ls.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.oreilly.servlet.ParameterNotFoundException;
import com.rqf.common.base.PathManager;
import com.rqf.common.base.ProjectInfor;
import com.rqf.ls.service.IFileUploadService;
import com.rqf.ls.service.ISignUpService;
import com.server.password.MyBase64;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;

@Controller
@RequestMapping("signUp")
public class SignUpController {
	
	@Autowired
	private ISignUpService signUpService;
	@Autowired
	private IFileUploadService fileUploadService;
	
	@RequestMapping("create/{phoneNo}")
	public void create(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable String phoneNo){
		Boolean isSuccess = true;
		String errorInfor = "";
		String activityName = request.getParameter("activityName");
		String locationAddr = request.getParameter("locationAddr");
		String locationDetail = request.getParameter("locationDetail");
		String note = request.getParameter("note");
		String userType = request.getParameter("userType");
		String imgFullName = request.getParameter("imgName");
		String longitude = request.getParameter("longitude");
		String latitude = request.getParameter("latitude");
		String players = request.getParameter("players");
		if (StringUtils.isEmpty(userType)) {
			userType = "normal";
		}
		
		//图片数据
		String base64Str = request.getParameter("base64");
		String[] split = base64Str.split("base64,");
		String tempStr = split[1];
		String realStr = tempStr.substring(0, tempStr.length() - 2);
		// FileUtil.base64Decode("/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2N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2ZJlO9icYwQe+efSpPGGnpD4eFwIEhJuF2qqgcbW/8ArflVpWMZO/Qj8BeIre0jbTL1xErPuikY4GT1U+ld1NZW0/Lxrk9xwa8OrX0nxLqmkKEtrjdCP+WUg3L/AIj8KHElOx3GpWYDzWzg7GBAz6HpT9PtRLNHCq4jXrjjAFYB+IEssYFzpcErDuHIH8jR/wALBlijK2umQRMe5ckf0qeVmntNDv4rS3txuSNRjnceSPxrz3x74gg1F49Ps3EkML73kHRmxgAewyfzrE1XxHqmrgrdXJ8r/nkg2r+Xf8ayqpKxk3c//9k=");

		byte[] decode = MyBase64.decode(realStr);
		
		// 1、保存公益活动数据
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("activityName", activityName);
		map.put("locationAddr", locationAddr);
		map.put("locationDetail", locationDetail);
		map.put("note", note);
		map.put("userType", userType);
		map.put("imgName", imgFullName);
		map.put("longitude", longitude);
		map.put("latitude", latitude);
		map.put("players", players);
		Map<String, Object> addInfo = signUpService.add(phoneNo, map);
		if (addInfo.size() <= 0) {
			isSuccess = false;
			errorInfor = "保存公益活动数据出错！";
			OutputUtil.returnSuccess(response, isSuccess, errorInfor);
			return;
		}

		Object idObj = addInfo.get("id");
		Object isSuccessObj = addInfo.get("isSuccess");
		if (isSuccessObj == null || "0".equals(isSuccessObj.toString())
				|| idObj == null || "".equals(idObj.toString())) {
			isSuccess = false;
			errorInfor = "保存公益活动数据出错！";
			OutputUtil.returnSuccess(response, isSuccess, errorInfor);
			return;
		}
		// 2、上传照片及保存数据库
		Map<String, Object> others = new HashMap<String, Object>();
		others.put("signUpId", idObj.toString());// 必须的
		others.put("saveType", PathManager.FILE_SAVE_TYPE_ACTIVITY);// 必须的

		try {
			isSuccess = fileUploadService.upload(ProjectInfor.PROJECT_NAME,
					phoneNo, PathManager.FILE_SAVE_TYPE_ACTIVITY, imgFullName,
					decode, others);
		} catch (ParameterNotFoundException e) {
			errorInfor = "图片上传出错！";
			e.printStackTrace();
		}
		
		response.addHeader("Access-Control-Allow-Origin", "*");
		OutputUtil.returnSuccess(response, isSuccess, errorInfor);
		
	}

	@RequestMapping("list/{phoneNo}")
	public void list(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable String phoneNo){
		String userType = request.getParameter("userType");
		if (StringUtils.isEmpty(userType)) {
			userType = "normal";
		}
		
		DataVector<Map<String, Object>> listData = signUpService.getList(phoneNo, userType, new RequestInput(request));
		List<Map<String, Object>> list = listData.getList();
		
		Iterator<Map<String, Object>> iterator = list.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object timeObj = next.get("createTime");
			if (timeObj != null) {
				next.put("CREATETIME", timeObj.toString());
			}
			Object idObj = next.get("ID");
			if (idObj != null) {
				String imgPath = signUpService.getImgPath(Integer.parseInt(idObj.toString()));
				next.put("imgPath", imgPath);
			}
		}
		String json = JSONSerielizer.toJson(list, null, null);
		response.addHeader("Access-Control-Allow-Origin", "*");
		OutputUtil.returnString(response, json);
	}
	@RequestMapping("form/{id}")
	public void form(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer id){
		Map<String, Object> form = signUpService.getForm(id);
		Object timeObj = form.get("createTime");
		if (timeObj != null) {
			form.put("CREATETIME", timeObj.toString());
		}
		String imgPath = signUpService.getImgPath(id);
		form.put("imgPath", imgPath);
		
		String json = JSONSerielizer.toJson(form, null, null);
		OutputUtil.returnJsonp(request, response, json);
	}
	@RequestMapping("img/{id}")
	public void img(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer id){
		String imgPath = signUpService.getImgPath(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("imgPath", imgPath);
		String json = JSONSerielizer.toJson(map, null, null);
		OutputUtil.returnJsonp(request, response, json);
	}
}
